//---------------------------------------------------------------------
// File: Exceptions.cs
// 
// Summary: Defines custom exceptions thrown by all 
//          classes and contains function for retrieving
//          exception strings
//
//
//---------------------------------------------------------------------
// This file is part of the Microsoft ESB Guidance for BizTalk
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft BizTalk
// Server 2006 R2 release and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Globalization;
using System.Reflection;
using System.Runtime.Serialization;
using Microsoft.Practices.ESB.Exception.Management;

namespace Microsoft.Practices.ESB.BizTalkOperations
{
    /// <summary>
    /// Summary description for ExceptionMessage.
    /// </summary>
    /// 
    internal static class Exceptions
    {
        #region Constants

        public const int BizTalkQueryApplicationExistsMessage = 205045;
        public const int BizTalkQueryApplicationNullMessage = 205032;
        public const int BizTalkQueryApplicationStatusNotFoundMessage = 205026;
        public const int BizTalkQueryCreateApplicationMessage = 205036;
        public const int BizTalkQueryCreateReceiveLocationMessage = 205041;
        public const int BizTalkQueryCreateReceivePortMessage = 205039;
        public const int BizTalkQueryCreateSendPortMessage = 205048;
        public const int BizTalkQueryDeleteApplicationMessage = 205038;
        public const int BizTalkQueryDeleteReceiveLocationMessage = 205043;
        public const int BizTalkQueryDeleteReceivePortMessage = 205047;
        public const int BizTalkQueryDeleteSendPortMessage = 205050;
        public const int BizTalkQueryGetAllApplicationsMessage = 205004;
        public const int BizTalkQueryGetApplicationStatusMessage = 205003;
        public const int BizTalkQueryGetHostsMessage = 205008;
        public const int BizTalkQueryGetOrchestrationsMessage = 205006;
        public const int BizTalkQueryGetPipelinesMessage = 205055;
        public const int BizTalkQueryGetReceiveLocationsMessage = 205010;
        public const int BizTalkQueryGetReceivePortsMessage = 205034;
        public const int BizTalkQueryGetSchemasMessage = 205053;
        public const int BizTalkQueryGetSendHandlerMessage = 205005;
        public const int BizTalkQueryGetSendPortsMessage = 205011;
        public const int BizTalkQueryGetStatusChangedMessage = 205001;
        public const int BizTalkQueryGetSystemStatusMessage = 205002;
        public const int BizTalkQueryInitSQLMessage = 205000;
        public const int BizTalkQueryIsManagedPipelineMessage = 205007;
        public const int BizTalkQueryLiveBodyNotFoundInstanceIdMessage = 205022;
        public const int BizTalkQueryLiveBodyNullStreamMessage = 205023;
        public const int BizTalkQueryLiveBodyUnexpectedMessage = 205024;
        public const int BizTalkQueryLiveMessageNotFoundMessage = 205029;
        public const int BizTalkQueryMessageFlowUnexpectedMessage = 205025;
        public const int BizTalkQueryMessageIdFormatMessage = 205028;
        public const int BizTalkQueryMessageInstanceMessage = 205014;
        public const int BizTalkQueryMessageNotTrackedMessage = 205033;
        public const int BizTalkQueryOrchestrationInstanceMessage = 205013;
        public const int BizTalkQueryOrchestrationsNotFoundMessage = 205027;
        public const int BizTalkQueryPipelineNullMessage = 205054;
        public const int BizTalkQueryReceiveByDescriptionMessage = 205009;
        public const int BizTalkQueryReceiveLocationExistsMessage = 205040;
        public const int BizTalkQueryReceivePortExistsMessage = 205044;
        public const int BizTalkQueryReceivesByDescriptionMessage = 205035;
        public const int BizTalkQueryRLNullMessage = 205030;
        public const int BizTalkQueryRPNullMessage = 205031;
        public const int BizTalkQuerySchemaNullMessage = 205052;
        public const int BizTalkQuerySendPortExistsMessage = 205051;
        public const int BizTalkQuerySendsByDescriptionMessage = 205012;
        public const int BizTalkQuerySPNullMessage = 205048;
        public const int BizTalkQueryTrackedMessageMessage = 205015;
        public const int BizTalkQueryTrackedMessageNullStreamMessage = 205016;
        public const int BizTalkQueryUpdateApplicationMessage = 205037;
        public const int BizTalkQueryUpdateReceiveDescriptionMessage = 205018;
        public const int BizTalkQueryUpdateReceiveDescriptionSqlMessage = 205019;
        public const int BizTalkQueryUpdateReceiveLocationMessage = 205042;
        public const int BizTalkQueryUpdateReceiveMessage = 205017;
        public const int BizTalkQueryUpdateReceivePortMessage = 205046;
        public const int BizTalkQueryUpdateSendDescriptionMessage = 205021;
        public const int BizTalkQueryUpdateSendDescriptionSqlMessage = 205020;
        public const int BizTalkQueryUpdateSendPortMessage = 205049;
        public const int BizTalkRegistryMissingMessage = 204999;
        public const int BizTalkQueryTransformNullMessage = 205056;
        public const int BizTalkQueryGetTransformsMessage = 205057;

        /// <summary>
        /// Error codes passed to custom Exceptions and used to look up
        /// descriptions in resource file
        /// </summary>
        public const string BTQueryResourceBaseName = "Microsoft.Practices.ESB.BizTalkOperations.Exceptions";

        public const int GeneralErrorMessage = 204011;

        #endregion

        /// <summary>
        /// looks up exceptions descriptions based on error codes. prepends "e"
        /// for lookup in resource file. Calls ResourceStringLoader.GetErrorDescription()
        /// </summary>
        /// <param name="errorCode">code represented by constant</param>
        /// <returns>string representing error code</returns>
        internal static string GetErrorDescription(int errorCode)
        {
            try
            {
                string message = ResourceStringLoader.GetErrorDescription(errorCode,
                                                                          BTQueryResourceBaseName,
                                                                          Assembly.GetExecutingAssembly());

                return message;
            }
            catch (System.Exception)
            {
                throw;
            }
        }
    }

    /// <summary>
    /// All exception classes used by the Resolver and Adapter Provider Framework are
    /// defined below.  All exceptions are derived from System.Exception
    /// </summary>
    [Serializable]
    public class BizTalkQueryInitSQLException : System.Exception
    {
        public BizTalkQueryInitSQLException()
            : base(Exceptions.GetErrorDescription(Exceptions.GeneralErrorMessage))
        {
        }

        public BizTalkQueryInitSQLException(string message)
            : base(message)
        {
        }

        public BizTalkQueryInitSQLException(string message, params Object[] args)
            : base(String.Format(CultureInfo.CurrentCulture, message, args), null)
        {
        }

        public BizTalkQueryInitSQLException(string message, System.Exception ex)
            : base(message, ex)
        {
        }

        public BizTalkQueryInitSQLException(int err)
            : base(String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err, Exceptions.GetErrorDescription(err))
                )
        {
        }

        public BizTalkQueryInitSQLException(int err, params Object[] args)
            : base(
                String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err,
                              String.Format(CultureInfo.CurrentCulture, Exceptions.GetErrorDescription(err), args)))
        {
        }

        protected BizTalkQueryInitSQLException(SerializationInfo serializationInfo, StreamingContext streamingContext)
            : base(serializationInfo, streamingContext)
        {
        }

        public new string Source
        {
            get { return base.Source; }
            set { base.Source = value; }
        }

        public new string Message
        {
            get { return base.Message; }
        }
    }

    [Serializable]
    public class BizTalkQueryInitializeException : System.Exception
    {
        public BizTalkQueryInitializeException()
            : base(Exceptions.GetErrorDescription(Exceptions.GeneralErrorMessage))
        {
        }

        public BizTalkQueryInitializeException(string message)
            : base(message)
        {
        }

        public BizTalkQueryInitializeException(string message, params Object[] args)
            : base(String.Format(CultureInfo.CurrentCulture, message, args), null)
        {
        }

        public BizTalkQueryInitializeException(string message, System.Exception ex)
            : base(message, ex)
        {
        }

        public BizTalkQueryInitializeException(int err)
            : base(String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err, Exceptions.GetErrorDescription(err))
                )
        {
        }

        public BizTalkQueryInitializeException(int err, System.Exception ex)
            : base(
                String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err,
                              String.Format(CultureInfo.CurrentCulture, Exceptions.GetErrorDescription(err))), ex)
        {
        }

        public BizTalkQueryInitializeException(int err, params Object[] args)
            : base(
                String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err,
                              String.Format(CultureInfo.CurrentCulture, Exceptions.GetErrorDescription(err), args)))
        {
        }

        protected BizTalkQueryInitializeException(SerializationInfo serializationInfo, StreamingContext streamingContext)
            : base(serializationInfo, streamingContext)
        {
        }

        public new string Source
        {
            get { return base.Source; }
            set { base.Source = value; }
        }

        public new string Message
        {
            get { return base.Message; }
        }
    }

    [Serializable]
    public class BizTalkQueryUnexpectedException : System.Exception
    {
        public BizTalkQueryUnexpectedException()
            : base(Exceptions.GetErrorDescription(Exceptions.GeneralErrorMessage))
        {
        }

        public BizTalkQueryUnexpectedException(string message)
            : base(message)
        {
        }

        public BizTalkQueryUnexpectedException(string message, params Object[] args)
            : base(String.Format(CultureInfo.CurrentCulture, message, args), null)
        {
        }

        public BizTalkQueryUnexpectedException(string message, System.Exception ex)
            : base(message, ex)
        {
        }

        public BizTalkQueryUnexpectedException(int err)
            : base(String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err, Exceptions.GetErrorDescription(err))
                )
        {
        }

        public BizTalkQueryUnexpectedException(int err, System.Exception ex)
            : base(
                String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err,
                              String.Format(CultureInfo.CurrentCulture, Exceptions.GetErrorDescription(err))), ex)
        {
        }

        public BizTalkQueryUnexpectedException(int err, System.Exception ex, params Object[] args)
            : base(
                String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err,
                              String.Format(CultureInfo.CurrentCulture, Exceptions.GetErrorDescription(err), args)), ex)
        {
        }

        public BizTalkQueryUnexpectedException(int err, params Object[] args)
            : base(
                String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err,
                              String.Format(CultureInfo.CurrentCulture, Exceptions.GetErrorDescription(err), args)))
        {
        }

        protected BizTalkQueryUnexpectedException(SerializationInfo serializationInfo, StreamingContext streamingContext)
            : base(serializationInfo, streamingContext)
        {
        }

        public new string Source
        {
            get { return base.Source; }
            set { base.Source = value; }
        }

        public new string Message
        {
            get { return base.Message; }
        }
    }

    [Serializable]
    public class BizTalkRegistryMissingException : System.Exception
    {
        public BizTalkRegistryMissingException()
            : base(Exceptions.GetErrorDescription(Exceptions.GeneralErrorMessage))
        {
        }

        public BizTalkRegistryMissingException(string message)
            : base(message)
        {
        }

        public BizTalkRegistryMissingException(string message, params Object[] args)
            : base(String.Format(CultureInfo.CurrentCulture, message, args), null)
        {
        }

        public BizTalkRegistryMissingException(string message, System.Exception ex)
            : base(message, ex)
        {
        }

        public BizTalkRegistryMissingException(int err)
            : base(String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err, Exceptions.GetErrorDescription(err))
                )
        {
        }

        public BizTalkRegistryMissingException(int err, params Object[] args)
            : base(
                String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err,
                              String.Format(CultureInfo.CurrentCulture, Exceptions.GetErrorDescription(err), args)))
        {
        }

        public BizTalkRegistryMissingException(int err, System.Exception ex, params Object[] args)
            : base(
                String.Format(CultureInfo.CurrentCulture, "Error {0}: {1}", err,
                              String.Format(CultureInfo.CurrentCulture, Exceptions.GetErrorDescription(err), args)), ex)
        {
        }

        protected BizTalkRegistryMissingException(SerializationInfo serializationInfo, StreamingContext streamingContext)
            : base(serializationInfo, streamingContext)
        {
        }

        public new string Source
        {
            get { return base.Source; }
            set { base.Source = value; }
        }

        public new string Message
        {
            get { return base.Message; }
        }
    }
}